/**
 * @author 徐楠
 * @date 2022/3/4 10:26
 * @version 1.0
 */

package com.xunan.likou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DuplicateDataInArray {
    public static void main(String[] args) {
        int[] nums = {4,3,2,7,8,2,3,1};
        List<Integer> duplicates = findDuplicates(nums);
        for (Integer duplicate : duplicates) {
            System.out.println(duplicate);
        }
    }

    public static List<Integer> findDuplicates(int[] nums) {
        List<Integer> list = new ArrayList<>();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
            if (map.get(nums[i]) == 2){
                list.add(nums[i]);
            }
        }
        return list;

    }
}
